﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using CookComputing.XmlRpc;

namespace AnOtherBlog.Core.HttpHandlers
{
	#region MetaWeblog API Structures

	[XmlRpcMissingMapping(MappingAction.Ignore)]
	public struct MetaWeblogEnclosure
	{
		public int length;
		public string type;
		public string url;
	}

	[XmlRpcMissingMapping(MappingAction.Ignore)]
	public struct MetaWeblogSource
	{
		public string name;
		public string url;
	}

	[XmlRpcMissingMapping(MappingAction.Ignore)]
	public struct MetaWeblogPost
	{
		[XmlRpcMissingMapping(MappingAction.Error)]
		[XmlRpcMember(Description = "Required when posting.")]
		public DateTime dateCreated;

		[XmlRpcMissingMapping(MappingAction.Error)]
		[XmlRpcMember(Description = "Required when posting.")]
		public string description;

		[XmlRpcMissingMapping(MappingAction.Error)]
		[XmlRpcMember(Description = "Required when posting.")]
		public string title;

		public string[] categories;
		public MetaWeblogEnclosure enclosure;		// new in 2.3.2.0
		public string link;		// new in 2.3.2.0
		public string permalink;

		[XmlRpcMember(Description = "Not required when posting. Depending on server may be either string or integer. Use Convert.ToInt32(postid) to treat as integer or Convert.ToString(postid) to treat as string")]
		public object postid;
		public MetaWeblogSource source;		// new in 2.3.2.0
		public string userid;

		public object mt_allow_comments;		// new in 2.3.2.0
		public object mt_allow_pings;		// new in 2.3.2.0
		public object mt_convert_breaks;		// new in 2.3.2.0
		public string mt_text_more;		// new in 2.3.2.0
		public string mt_excerpt;		// new in 2.3.2.0

		public string wp_slug;		//	old, estaba en la version anterior me parece que es para WordPress
	}

	//[Serializable]		// old, estaba en la version anterior
	public struct MetaWeblogCategoryInfo
	{
		public string description;
		public string htmlUrl;
		public string rssUrl;
		public string title;
		public string categoryid;
	}

	public struct MetaWeblogCategory
	{
		public string categoryId;
		public string categoryName;
	}

	// Equivalente a MediaObjet de la versión anterior
	[XmlRpcMissingMapping(MappingAction.Ignore)]
	public struct MetaWeblogFileData
	{
		public byte[] bits;
		public string name;
		public string type;
	}


	// Equivalente a MediaObjectInfo de la versión anterior
	[Serializable]
	public struct MetaWeblogUrlData
	{
		public string url;
	}

	#endregion

	#region Blogger API Structures

	public struct MetaWeblogBlogInfo
	{
		public string blogid;
		public string url;
		public string blogName;
	}

	public struct MetaWeblogUserInfo
	{
		public string userid;
		public string firstname;
		public string lastname;
		public string nickname;
		public string email;
		public string url;
	}

	#endregion


	public interface IMetaWeblog
	{
		#region MetaWeblog API Interface

		/// <summary>
		/// Agrega una publicación en el blog indicado
		/// </summary>
		/// <param name="blogid">Identificador del blog</param>
		/// <param name="username">Usuario</param>
		/// <param name="password">Password</param>
		/// <param name="post">Publicación</param>
		/// <param name="publish">Publicar (si/no)</param>
		/// <returns>Identificador de la publicación como cadena</returns>
		[XmlRpcMethod("metaWeblog.newPost", Description = "Makes a new post to a designated blog using the metaWeblog API. Returns postid as a string.")]
		string AddPost(string blogid, string username, string password, MetaWeblogPost post, bool publish);

		[XmlRpcMethod("metaWeblog.editPost", Description = "Updates and existing post to a designated blog using the metaWeblog API. Returns true if completed.")]
		object UpdatePost(string postid, string username, string password, MetaWeblogPost post, bool publish);

		[XmlRpcMethod("metaWeblog.getPost", Description = "Retrieves an existing post using the metaWeblog API. Returns the metaWeblog struct.")]
		MetaWeblogPost GetPost(string postid, string username, string password);

		[XmlRpcMethod("metaWeblog.getCategories", Description = "Retrieves a list of valid categories for a post using the metaWeblog API. Returns the metaWeblog categories struct collection.")]
		MetaWeblogCategoryInfo[] GetCategories(string blogid, string username, string password);

		[XmlRpcMethod("metaWeblog.getRecentPosts", Description = "Retrieves a list of the most recent existing post using the metaWeblog API. Returns the metaWeblog struct collection.")]
		MetaWeblogPost[] GetRecentPosts(string blogid, string username, string password, int numberOfPosts);

		[XmlRpcMethod("metaWeblog.newMediaObject", Description = "Makes a new file to a designated blog using the metaWeblog API. Returns url as a string of a struct.")]
		MetaWeblogUrlData NewMediaObject(string blogid, string username, string password, MetaWeblogFileData fileData);

		#endregion

		#region Blogger API Interface

		[XmlRpcMethod("blogger.deletePost")]
		[return: XmlRpcReturnValue(Description = "Returns true.")]
		bool DeletePost(string key, string postid, string username, string password, bool publish);

		[XmlRpcMethod("blogger.getUsersBlogs")]
		MetaWeblogBlogInfo[] GetUsersBlogs(string key, string username, string password);

		[XmlRpcMethod("blogger.getUserInfo")]
		MetaWeblogUserInfo GetUserInfo(string key, string username, string password);

		#endregion

	}

}
